package com.hs.panghu.dao.impl;

import com.hs.panghu.dao.GoodsDao;
import com.hs.panghu.entity.Goods;
import com.hs.panghu.util.JDBCDruidUtil;
import com.mysql.cj.x.protobuf.MysqlxResultset;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class GoodsDaoImpl implements GoodsDao {


    @Override
    public int modifyShopGoods(Integer goodsId) {
        String sql="update goods set goodStatus=abs(goodStatus-1) where goodsId=?";

        Object[]objects= {goodsId};
        int row= JDBCDruidUtil.update(sql, objects);

        return row;
    }


    @Override
    public List<Goods> selectGoods(Map<String, String> map) {
        List<Goods>list=new ArrayList<>();
        StringBuilder sb=new StringBuilder("select*from goods where 1=1 ");
        List params=new ArrayList();


        //所属门店
         String shopName = map.get("keyword");
        if (shopName!=null&&!"".equals(shopName)){
            sb.append(" and goodsShopName like ? ");
            params.add("%"+shopName+"%");
        }
        //商品名字
        String goodsName = map.get("goodsName");
        if (goodsName!=null&&!"".equals(goodsName)){
            sb.append(" and goodsName like ? ");
            params.add("%"+goodsName+"%");
        }
        //日期
        String startTime = map.get("startTime");
        String endTime = map.get("endTime");
        if (startTime!=null&&!"".equals(startTime)){
            sb.append(" and addTime between ? and ? ");
            params.add(startTime);
            params.add(endTime);
        }
        //商品状态
        String goodsStatus = map.get("goodsStatus");
        if (goodsStatus!=null&&!"".equals(goodsStatus)){
            sb.append(" and goodsStatus=? ");
            params.add(goodsStatus);
        }

        //分页
        Integer pageNum = Integer.parseInt(map.get("pageNum"));
        Integer pageSize = Integer.parseInt(map.get("pageSize"));

        sb.append(" limit ?,? ");
        params.add((pageNum-1)*pageSize);
        params.add(pageSize);

        String sql =sb.toString();
        System.out.println(sql);
        Object[]objects=params.toArray();

        ResultSet query = JDBCDruidUtil.query(sql, objects);

        try {
            while (query.next()){
                Goods goods=new Goods(
                        query.getInt(1),
                        query.getString(2),
                        query.getString(3),
                        query.getInt(4),
                        query.getString(5),
                        query.getInt(6),
                        query.getString(7),
                        query.getDate(8),
                        query.getString(9),
                        query.getString(10),
                        query.getString(11),
                        query.getInt(12)
                );
                list.add(goods);
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }finally {
            JDBCDruidUtil.close();
        }

        return list;
    }

    @Override
    public int selectGoodsCount(Map<String, String> map) {
        int count=0;
        StringBuilder sb=new StringBuilder("select count(1) from goods where 1=1 ");
        List params=new ArrayList();


        //所属门店
        String shopName = map.get("keyword");
        if (shopName!=null&&!"".equals(shopName)){
            sb.append(" and goodsShopName like ? ");
            params.add("%"+shopName+"%");
        }
        //商品名字
        String goodsName = map.get("goodsName");
        if (goodsName!=null&&!"".equals(goodsName)){
            sb.append(" and goodsName like ? ");
            params.add("%"+goodsName+"%");
        }
        //日期
        String startTime = map.get("startTime");
        String endTime = map.get("endTime");
        if (startTime!=null&&!"".equals(startTime)){
            sb.append(" and addTime between ? and ? ");
            params.add(startTime);
            params.add(endTime);
        }
        //商品状态
        String goodsStatus = map.get("goodsStatus");
        if (goodsStatus!=null&&!"".equals(goodsStatus)){
            sb.append(" and goodsStatus=? ");
            params.add(goodsStatus);
        }


        String sql =sb.toString();
        System.out.println(sql);
        Object[]objects=params.toArray();

        ResultSet query = JDBCDruidUtil.query(sql, objects);

        try {
            while (query.next()){
                count=query.getInt(1);

            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }finally {
            JDBCDruidUtil.close();
        }
        return count;
    }
}
